from orthogonalization import orthogonalize
from orthogonalization import aug_orthogonalize
from math import sqrt
from vec import Vec
from matutil import coldict2mat
from matutil import rowdict2mat
from matutil import mat2rowdict
from matutil import mat2coldict
 
def normalize(v):return v/sqrt(v*v)

def adjust(v,multipliers): return [multipliers[i]*v[i] for i in range(v.D)]
     
def orthonormalize(L):
    '''
    Input: a list L of linearly independent Vecs
    Output: A list T of orthonormal Vecs such that for all i in [1, len(L)],
            Span L[:i] == Span T[:i]
    '''
    l = orthogonalize(L)
    return [normalize(v) for v in l]
    pass

def norm(v):return sqrt(v*v)

def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
    '''
    vstarlist,sigmavec = aug_orthogonalize(L)
    Q = orthonormalize(vstarlist)
    R = mat2rowdict(coldict2mat(sigmavec))
    R = [norm(vstarlist[i])*R[i] for i in range(len(vstarlist))]
    R = list(mat2coldict(rowdict2mat(R)).values())
    return Q,R
    
    
    #R = 
    pass
